<template>
  <div class="address-container">
    <!-- 左侧侧边栏 -->
    <div class="sidebar">
      <PersonalCenterSidebar />
    </div>

    <!-- 右侧内容区域 -->
    <div class="content-area">
      <!-- 卡片区域 -->
      <div class="card-container">
        <div class="card" v-for="address in tableData" :key="address.id">
          <div class="card-header">
            <span>ID: {{ address.id }}</span>
            <span>收件人: {{ address.receiverName }}</span>
          </div>
          <div class="card-body">
            <p><strong>电话:</strong> {{ address.receiverPhone }}</p>
            <p><strong>省:</strong> {{ address.province }}</p>
            <p><strong>市:</strong> {{ address.city }}</p>
            <p><strong>区/县:</strong> {{ address.district }}</p>
            <p><strong>详细地址:</strong> {{ address.addressDetail }}</p>
            <p><strong>邮编:</strong> {{ address.zipCode }}</p>
            <p><strong>默认地址:</strong> {{ address.default ? '是' : '否' }}</p>
          </div>
          <div class="card-footer">
            <el-button type="text" @click="handleEdit(address)">编辑</el-button>
            <el-popconfirm title="确认删除吗?" @confirm="handleDelete(address.id)">
              <template #reference>
                <el-button type="text">删除</el-button>
              </template>
            </el-popconfirm>
          </div>
        </div>
      </div>

      <!-- 返回按钮 -->
      <div class="back-button-container">
        <el-button type="primary" @click="add">新增</el-button>
        <el-button type="primary" @click="goBack">返回</el-button>
      </div>
    </div>

    <!-- 点击新增，弹出的对话框 -->
    <el-dialog
        v-model="dialogVisible"
        title="地址管理"
        width="30%"
        :before-close="handleClose"
    >
      <el-form :model="form" label-width="120px">
        <el-form-item label="用户ID">
          <el-input v-model="form.userId" style="width: 80%"/>
        </el-form-item>
        <el-form-item label="收件人">
          <el-input v-model="form.receiverName" style="width: 80%"/>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.receiverPhone" style="width: 80%"/>
        </el-form-item>
        <el-form-item label="省">
          <el-input v-model="form.province" style="width: 80%"/>
        </el-form-item>
        <el-form-item label="市">
          <el-input v-model="form.city" style="width: 80%"/>
        </el-form-item>
        <el-form-item label="区/县">
          <el-input v-model="form.district" style="width: 80%"/>
        </el-form-item>
        <el-form-item label="详细地址">
          <el-input v-model="form.addressDetail" style="width: 80%"/>
        </el-form-item>
        <el-form-item label="邮编">
          <el-input v-model="form.zipCode" style="width: 80%"/>
        </el-form-item>
        <el-form-item label="是否默认">
          <el-checkbox v-model="form.defaultCheckbox" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="save">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";
import router from "@/router"; // 引入路由实例
import PersonalCenterSidebar from "@/views/PersonalCenterSidebar.vue";

export default {
  name: "Address",
  components: {PersonalCenterSidebar},
  data() {
    return {
      form: {},
      dialogVisible: false,
      search: '',
      currentPage: 1,
      total: 10,
      pageSize: 10,
      tableData: [],
      roomNameQuery: '', // 新增房间名称查询条件变量
      selectedIds: [], // 存储选中的行 ID
      defaultCheckbox: false, // 用于控制是否默认的 checkbox 状态
      showPagination: true // 默认显示分页栏，可以根据需要设置为 false 来隐藏
    }
  },
  methods: {
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.defaultCheckbox = this.form.default === 1;
      this.dialogVisible = true;
    },
    handleDelete(id) {
      request.delete("/user/deleteById/" + id).then(res => {
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: '删除成功'
          })
        } else {
          this.$message({
            type: "error",
            message: "删除失败"
          })
        }
        this.load();
      })
    },
    handleSizeChange() {
      this.load();
    },
    handleCurrentChange() {
      this.load();
    },
    add() {
      this.dialogVisible = true;
      this.form = {};
      this.defaultCheckbox = false;
    },
    save() {
      this.form.default = this.defaultCheckbox ? 1 : 0;
      if (this.form.id) {
        request.put("/user/updateAddress", this.form).then(res => {
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "更新成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load();
          this.dialogVisible = false;
        })
      } else {
        request.post("/user/addAdderss", this.form).then(res => {
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "新增成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load();
          this.dialogVisible = false;
        })
      }
    },
    searchByRoomName() {
      request.get("/hotelRoom/getHotelRoomByName", {
        params: {
          roomName: this.roomNameQuery
        }
      }).then(res => {
        this.tableData = res.data.list;
        this.total = res.data.total;
      });
    },
    load() {
      request.get("/user/getAdderssPage", {
        params: {
          pageNumber: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res => {
        this.tableData = res.data.list;
        this.total = res.data.total;
      })
    },
    batchDelete() {
      if (this.selectedIds.length === 0) {
        this.$message({
          type: 'warning',
          message: '请先选择要删除的行'
        });
        return;
      }
      request.delete("/hotelRoom/deleteHotelRoomsByIds", {
        data: this.selectedIds
      }).then(res => {
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: '批量删除成功'
          });
          this.load();
          this.selectedIds = [];
        } else {
          this.$message({
            type: "error",
            message: "批量删除失败"
          });
        }
      });
    },
    handleSelectionChange(selection) {
      this.selectedIds = selection.map(item => item.id);
    },
    goBack() {
      router.go(-1);
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {
          });
    }
  },
  created() {
    this.load();
  }
}
</script>

<style scoped>
/* 容器样式 */
.address-container {
  display: flex;
  background-color: #f9f9f9;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  max-width: 1200px;
  margin: 0 auto;
}

/* 左侧侧边栏样式 */
.sidebar {
  width: 250px;
  margin-right: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

/* 右侧内容区域样式 */
.content-area {
  flex-grow: 1;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

/* 卡片区域样式 */
.card-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
  margin-top: 20px;
}

.card {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 20px;
  transition: transform 0.3s;
}

.card:hover {
  transform: translateY(-5px);
}

.card-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  font-weight: bold;
}

.card-body {
  p {
    margin: 5px 0;
  }
}

.card-footer {
  display: flex;
  justify-content: space-around;
  margin-top: 10px;
}

/* 返回按钮容器样式 */
.back-button-container {
  text-align: center;
  margin-top: 20px;
}

/* 按钮样式 */
.el-button {
  margin: 0 5px;
  transition: all 0.3s;
}

.el-button:hover {
  transform: scale(1.05);
}

/* 输入框样式 */
.el-input {
  transition: border-color 0.3s;
}

.el-input:focus-within {
  border-color: #409eff;
}

/* 对话框样式 */
.el-dialog {
  border-radius: 10px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.el-dialog__header {
  background-color: #f0f0f0;
  padding: 15px;
  border-bottom: 1px solid #ddd;
  border-radius: 10px 10px 0 0;
}

.el-dialog__body {
  padding: 20px;
}

.el-dialog__footer {
  padding: 10px 20px;
  border-top: 1px solid #ddd;
  border-radius: 0 0 10px 10px;
  text-align: right;
}
</style>